--*********************************************************************
--*  Exports the specified physics of a mesh to the engines file format
--*  
fn exportStaticPhys  = 
(
	
	openOutputFile ((getFilenameFile maxFileName) +  ".phy")
	writeshort gFilestream 200
	
	for oPhysics in selection do
	(
			
		local pObjectType = (oPhysics.baseObject as string);
	--	messagebox pObjectType
		
		if pObjectType == "Dummy" then
			continue
			
		if(pObjectType == "Box") then
		(
			if (writeshort gFilestream 1) == false then
				print "error"
			
			
			local scalePart = (oPhysics.transform.scalepart)
			local dimensions = [oPhysics.width *  scalePart[1], oPhysics.height * scalePart[3], oPhysics.length *  scalePart[2]]
			print (dimensions  as String)
			
			print "_________"

			print (oPhysics.transform.translationpart as string)
			print (scalePart as string)
			print (oPhysics.transform.translationpart * scalePart) as string
				
		--	local scaleoffset = [((scalePart[1] - 1) * oPhysics.width * 0.5) as Float, ((scalePart[3] - 1) * oPhysics.height * 0.5) as Float, ((scalePart[2] - 1) * oPhysics.length * 0.5) as Float] 
		
			local tOrrientTranslate = (oPhysics.transform.rotationpart) as Matrix3
			local translationPart = oPhysics.transform.translationpart
			translationPart[3] = translationPart[3] + (dimensions[2] * 0.5)
			
			tOrrientTranslate.translation =  (translationPart) as Point3
			WriteMatrix tOrrientTranslate
			WriteFloatSafe dimensions[1]
			WriteFloatSafe dimensions[2]
			WriteFloatSafe dimensions[3]
		
			print "box written.."	
		)
		else if (pObjectType == "Capsule") then 
		(
			if (writeshort gFilestream 4) == false then
				print "error"
			
			
			local scalePart = (oPhysics.transform.scalepart)
			local tOrrientTranslate = (oPhysics.transform.rotationpart) as Matrix3
			local translationPart = oPhysics.transform.translationpart
			translationPart[3] = translationPart[3] + (oPhysics.height * scalePart[3] * 0.5)
			
			tOrrientTranslate.translation =  (translationPart) as Point3
			WriteMatrix tOrrientTranslate
			WriteFloatSafe oPhysics.radius
			WriteFloatSafe oPhysics.height
		
			print "capsule written.."	
			
		)
		else if(pObjectType == "Sphere") then 
		(
			if (writeshort gFilestream 2) == false then
				print "error"
			
			local tOrrientTranslate = (oPhysics.transform.rotationpart) as Matrix3
			tOrrientTranslate.translation = oPhysics.transform.translationpart
			WriteMatrix tOrrientTranslate
			
			
			local scalePart = (oPhysics.transform.scalepart)
			local scaleAvg = ((scalePart[1] + scalePart[2] + scalePart[3]) / 3.0) as Float
			
			if (writefloat gFilestream (oPhysics.radius * scaleAvg)) == false then
				print "error"
			
			print "written sphere"
		)
		else
		(
			if (writeshort gFilestream 3) == false then
				print "error"
			--ALREADY TRANSFORMED??
			--local tOrrientTranslate = (oPhysics.transform.rotationpart) as Matrix3
			local invdoubleZ = 0 * oPhysics.transform.translationpart[3]
			WriteMatrix (Matrix3 1) --tOrrientTranslate
			safeMesh = convertToMesh (snapshot oPhysics)
			
			if safeMesh != undefined then
			(
				
				if (writeshort gFilestream safeMesh.numFaces) == false then
					print "error"
				
				for f = 1 to safeMesh.numFaces do
				(
			--	messageBox (f as string)	
					local face = getFace safeMesh f
					for i = 1 to 3 do
					(
						local position = getVert safeMesh face[i]
			WriteFloatSafe position[1]
			WriteFloatSafe position[3]
			WriteFloatSafe (position[2] + invdoubleZ)
					)
				)
			
				delete safeMesh 
			)
			
		)
	)
	--This is like a checksum
--	print "written checksum"
	writeshort gFilestream 101
	print "closing physics file"
	closeOutputFile()
) 